package pl.lodz.p.mbus;

import android.content.Context;
import android.view.Display;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ListAdapter;

public class Utility {

    public static void setListViewHeightBasedOnChildren(HorizontalListView listView, int maxWidth) {
    	
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalWidth = 0;
        int desiredHeight = MeasureSpec.makeMeasureSpec(listView.getHeight(), MeasureSpec.AT_MOST);
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(MeasureSpec.UNSPECIFIED, desiredHeight);
            totalWidth += listItem.getMeasuredWidth();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.width = totalWidth;
        if (totalWidth>=maxWidth)
        	params.width=-1;
        listView.setLayoutParams(params);
        listView.requestLayout();
    }


 }